import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.LocalStorage 2.15
import QtQuick.Layouts 1.15

ApplicationWindow {
    width: 800
    height: 600
    visible: true
    title: qsTr("文件操作示例")

    // 自定义组件 文件读取
    FileIO {
        id: fileIO
        // source: "qrc:/example.text"
        // source: "/home/zk/qml-learning/file/example.text"
    }

    // 创建标签页
    TabBar {
        id: tabBar
        width: parent.width

        TabButton {
            text: "文件读取"
        }
        TabButton {
            text: "本地存储"
        }
        TabButton {
            text: "文件信息"
        }
    }

    // 标签页内容
    StackLayout {
        anchors.top: tabBar.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.margins: 10
        currentIndex: tabBar.currentIndex

        // 文件读取页面
        Page {
            Column {
                anchors.centerIn: parent
                spacing: 20

                TextArea {
                    id: textArea
                    width: 400
                    height: 200
                    text: fileIO.text
                }

                Button {
                    text: "读取文件"
                    onClicked: {
                        fileIO.read();
                    }
                }

                Text {
                    text: fileIO.error ? fileIO.errorString : "读取成功"
                    color: fileIO.error ? "red" : "green"
                }
            }
        }

        // 本地存储页面
        Page {
            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 10
                spacing: 10

                // 输入区域
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 10

                    TextField {
                        id: keyInput
                        Layout.fillWidth: true
                        placeholderText: "输入键"
                    }

                    TextField {
                        id: valueInput
                        Layout.fillWidth: true
                        placeholderText: "输入值"
                    }
                }

                // 按钮区域
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 10

                    Button {
                        text: "保存"
                        Layout.fillWidth: true
                        onClicked: {
                            if (keyInput.text && valueInput.text) {
                                setSetting(keyInput.text, valueInput.text)
                                resultText.text = "已保存: " + keyInput.text + " = " + valueInput.text
                            }
                        }
                    }

                    Button {
                        text: "读取"
                        Layout.fillWidth: true
                        onClicked: {
                            if (keyInput.text) {
                                var value = getSetting(keyInput.text, "未找到")
                                valueInput.text = value
                                resultText.text = "读取结果: " + keyInput.text + " = " + value
                            }
                        }
                    }
                }

                // 显示区域
                ScrollView {
                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    Text {
                        id: resultText
                        width: parent.width
                        wrapMode: Text.WordWrap
                        text: "操作结果将显示在这里"
                    }
                }
            }
        }

        // 文件信息页面
        Page {
            Column {
                anchors.fill: parent
                anchors.margins: 10
                spacing: 10

                Button {
                    text: "选择文件"
                    onClicked: {
                        fileInput.click()
                    }
                }

                // 隐藏元素输入
                Rectangle {
                    id: fileInput
                    visible: false
                    Component.onCompleted: {

                    }
                }

                ListView {
                    id: fileList
                    width: parent.width
                    height: 200
                    model: ListModel{}

                    delegate: Column {
                        width: parent.width
                        spacing: 10

                        Text {
                            text: "文件名: " + model.fileName
                        }
                        Text {
                            text: "文件大小: " + model.fileSize + " bytes"
                        }
                        Text {
                            text: "文件类型: " + model.fileType
                        }
                        Rectangle {
                            width: parent.width
                            height: 1
                            color: "gray"
                        }
                    }
                }

                // 模拟添加文件信息
                Button {
                    id: bt1
                    text: "添加文件"
                    onClicked: {
                        fileList.model.append({
                            fileName: "example.txt",
                            fileSize: 1024,
                            fileType: "text/plain"
                        })
                    }
                }
            }
        }
    }

    // 数据库功能
    function initDatabase() {
        var db = LocalStorage.openDatabaseSync("MyDate", "1.0", "本地存储示例", 1000000)
        db.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS Test (id unique, name)')
        })
    }

    function setSetting(key, value) {
        var db = LocalStorage.openDatabaseSync("MyDate", "1.0", "本地存储示例", 1000000)
        db.transaction(function(tx) {
            tx.executeSql('INSERT OR REPLACE INTO Test (id, name) VALUES (?, ?)', [key, value])
        })
    }

    function getSetting(key, defaultValue) {
        var db = LocalStorage.openDatabaseSync("MyDate", "1.0", "本地存储示例", 1000000)
        var value = defaultValue
        db.transaction(function(tx) {
            var result = tx.executeSql('SELECT name FROM Test WHERE id=?', [key])
            if (result.rows.length > 0) {
                value = result.rows.item(0).name
            }
        })
        return value
    }

    Component.onCompleted: {
        initDatabase()
    }
}